﻿namespace AntBot
{
	public class Move
	{
		public enum MoveType
		{
			None = 0,
			FoodCollection = 1
		}

		public MoveType Type;
		public Route Route;
		public int TurnsBefore;

		public Move(Route route, MoveType type)
			: this(route, type, 0)
		{}

		public Move(Route route, MoveType type, int turnsBefore)
		{
			Route = route;
			Type = type;
			TurnsBefore = turnsBefore;
		}

		public State.Directions GetFirstStep()
		{
			return Route == null ? State.Directions.Unknown : Route.GetFirstStepDirection();
		}

		private string TurnsBeforeToString()
		{
			return TurnsBefore == 0 ? "" : "wait " + TurnsBefore;
		}

		public override string ToString()
		{
			return
				Route + TurnsBeforeToString();
			
		}

		public string ToStringDetail()
		{
			return
				Route.ToStringDetail() + TurnsBeforeToString() +
				(Type == MoveType.FoodCollection ? " FC" : " ");

		}

		public static bool operator ==(Move r1, Move r2)
		{
			if (ReferenceEquals(r1, r2))
			{
				// handles if both are null as well as object identity
				return true;
			}

			if ((object)r1 == null || (object)r2 == null)
				return false;

			return ((r1.Route == r2.Route) && (r1.TurnsBefore == r2.TurnsBefore));
		}

		public static bool operator !=(Move r1, Move r2)
		{
			return !(r1 == r2);
		}

		public override bool Equals(object obj)
		{
			if (obj == null /*|| GetType() != obj.GetType()*/)
				return false;

			return this == (Move)obj;
		}

		public override int GetHashCode()
		{
			int fromHashCode = Route == null ? -1 : Route.FromLoc.GetHashCode();
			int toHashCode = Route == null ? -1 : Route.ToLoc.GetHashCode();
			return fromHashCode + toHashCode + TurnsBefore;
		}
	}
}
